import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ModeSelectionScreen extends StatefulWidget {
  const ModeSelectionScreen({super.key});

  @override
  State<ModeSelectionScreen> createState() => _ModeSelectionScreenState();
}

class _ModeSelectionScreenState extends State<ModeSelectionScreen> {

  @override
  void initState() {
    super.initState();
    _checkFirstLaunch();
  }

  Future<void> _checkFirstLaunch() async {
    final prefs = await SharedPreferences.getInstance();
    final isFirstLaunch = prefs.getBool('isFirstLaunch') ?? true;
    
    
    if (isFirstLaunch) {
      _selectPeriodMode();
    }
  }

  Future<void> _selectPeriodMode() async {
    final prefs = await SharedPreferences.getInstance();
    
    
    await prefs.setBool('isFirstLaunch', false);
    await prefs.setString('selectedMode', 'period');
    
    
    await _initializePeriodData();
    
    
    if (mounted) {
      Navigator.pushReplacementNamed(context, '/profile_completion', arguments: 'period');
    }
  }

  Future<void> _selectMode(String mode) async {
    final prefs = await SharedPreferences.getInstance();
    
    
    await prefs.setBool('isFirstLaunch', false);
    await prefs.setString('selectedMode', mode);
    
    
    if (mode == 'period') {
      await _initializePeriodData();
    }
    
    
    if (mounted) {
      Navigator.pushReplacementNamed(context, '/profile_completion', arguments: mode);
    }
  }

  Future<void> _initializePeriodData() async {
    final prefs = await SharedPreferences.getInstance();
    
    
    await prefs.setInt('cycleLength', 28); 

    await prefs.setInt('periodLength', 5);  

    
    
    final lastPeriodStart = DateTime.now().subtract(const Duration(days: 14));
    await prefs.setString('lastPeriodStart', lastPeriodStart.toIso8601String());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          children: [
            Container(
              padding: const EdgeInsets.only(top: 40.0, bottom: 20.0),
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                      Color.fromRGBO(255, 243, 248, 1),
                      Color.fromRGBO(255, 255, 255, 1),
                    ],
                ),
              ),
              child: const Center(
                child: Text(
                  '请选择模式',
                  style: TextStyle(
                    fontSize: 24.0,
                    fontWeight: FontWeight.bold,
                    color: Color.fromRGBO(0, 0, 0, 1),
                  ),
                ),
              ),
            ),
            Expanded(
              child: ListView(
                padding: const EdgeInsets.all(20.0),
                children: [
                  _buildModeItem(
                    context,
                    icon: Icons.calendar_today,
                    iconBgColor: const Color.fromRGBO(255, 221, 235, 1),
                    iconColor: const Color.fromRGBO(238, 105, 164, 1),
                    title: '经期',
                    description: '经期管理、智能预测、健康分析',
                    onTap: () => _selectMode('period'),
                  ),
                  const SizedBox(height: 16.0),
                  _buildModeItem(
                    context,
                    icon: Icons.favorite,
                    iconBgColor: const Color.fromRGBO(238, 228, 251, 1),
                    iconColor: const Color.fromRGBO(156, 89, 246, 1),
                    title: '备孕',
                    description: '怀孕几率、排卵预测、爱爱建议',
                    onTap: () => _selectMode('pregnancy_prep'),
                  ),
                  const SizedBox(height: 16.0),
                  _buildModeItem(
                    context,
                    icon: Icons.pregnant_woman,
                    iconBgColor: const Color.fromRGBO(228, 245, 248, 1),
                    iconColor: const Color.fromRGBO(92, 184, 200, 1),
                    title: '怀孕',
                    description: '胎儿每日变化、食物能不能吃',
                    onTap: () => _selectMode('pregnancy'),
                  ),
                  const SizedBox(height: 16.0),
                  _buildModeItem(
                    context,
                    icon: Icons.child_care,
                    iconBgColor: const Color.fromRGBO(237, 253, 237, 1),
                    iconColor: const Color.fromRGBO(102, 187, 106, 1),
                    title: '育儿',
                    description: '宝宝成长相册、育儿知识、经期预测',
                    onTap: () => _selectMode('parenting'),
                  ),
                  const SizedBox(height: 16.0),
                  _buildModeItem(
                    context,
                    icon: Icons.phone_android,
                    iconBgColor: const Color.fromRGBO(255, 249, 237, 1),
                    iconColor: const Color.fromRGBO(255, 193, 7, 1),
                    title: '我是被邀请的',
                    description: '共同参与备孕、怀孕、育儿',
                    onTap: () => _selectMode('invited'),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildModeItem(
    BuildContext context,
    {
      required IconData icon,
      required Color iconBgColor,
      required Color iconColor,
      required String title,
      required String description,
      required VoidCallback onTap,
    }
  ) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12.0),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withValues(alpha: 0.1),
            spreadRadius: 1,
            blurRadius: 3,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: ListTile(
        onTap: onTap,
        leading: Container(
          width: 50.0,
          height: 50.0,
          decoration: BoxDecoration(
            color: iconBgColor,
            borderRadius: BorderRadius.circular(12.0),
          ),
          child: Icon(
            icon,
            color: iconColor,
            size: 24.0,
          ),
        ),
        title: Text(
          title,
          style: const TextStyle(
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
        subtitle: Text(
          description,
          style: TextStyle(
            fontSize: 13.0,
            color: Colors.grey[600],
          ),
        ),
        trailing: Icon(
          Icons.arrow_forward_ios,
          color: Colors.grey[400],
          size: 16.0,
        ),
      ),
    );
  }
}